package com.bj.disc.util

import java.io.File
import kotlin.random.Random

//oc 每一关就是一轮  就是8关就是8轮
object GameOcUtil : BaseGameUtil() {
    fun getGameOcResult(games: List<GameOc>, level: Int): GameOc? {
        return games.find { it.level == level }
    }
    //根据文件路径生成所有 例如static/oc/level1/images
    fun generateGames(path: String): List<GameOc> {
        val levelFolder = File(path)
        var themeFolders =
            levelFolder.listFiles()?.filter { it.isDirectory }?.map { it.name }?.toMutableList()

        val games = mutableListOf<GameOc>()

        if (themeFolders != null && themeFolders.isNotEmpty()) {
            themeFolders=getRandomSubset(themeFolders,themeFolders.size)
            for (level in 3..8) {
                var currentTheme = themeFolders[level - 3]
                val themeImageFile = File("$levelFolder/$currentTheme/1Theme.png")
                val themeFolder = File("$levelFolder/$currentTheme")
                val themeFiles = themeFolder.listFiles()?.toList()
//                for (round in 1..4) {
                if (themeFiles != null && themeFiles.size >= 5) {
                    val shuffledThemeFiles = themeFiles.shuffled().filter { !it.name.contains("1Theme",true) }
                    val correctAnswer = shuffledThemeFiles.filter {  !it.name.equals("Thumbs.db") }.take(4).map { it.path }
                    val incorrectAnswer = mutableListOf<String>()

                    val result = themeFolders.filterIndexed { index, _ -> index != (level - 3) }

                    for(i in 0..7){
                        val incorrectTheme = result.get(i)
                        val incorrectThemeFolder = File("$levelFolder/$incorrectTheme")
                        val incorrectThemeFiles = incorrectThemeFolder.listFiles().filter { !it.name.equals("Thumbs.db") }?.toList()
                        if (incorrectThemeFiles != null && incorrectThemeFiles.isNotEmpty()) {
                            val incorrectFile = incorrectThemeFiles.random()
                            incorrectAnswer.add(incorrectFile.path)
                        }
                    }

//                    if(Oc?.ocTheme?.get(currentTheme)?.isNotEmpty() == true) {
//                        currentTheme = Oc.ocTheme[currentTheme].toString()
//                    }
                    games.add(
                        GameOc(
                            level-2,
                            1,
                            currentTheme,
                            themeImageFile.path,
                            correctAnswer,
                            incorrectAnswer
                        )
                    )
                } else {
                    println("主题文件夹 $currentTheme 中的文件数量不足。")
                }
            }
            // }
        } else {
            println("level 文件夹为空或不存在主题文件夹。")
        }

        return games
    }

    fun generateGames(path: String, currentTheme: String): GameOc?{
        val levelFolder = File(path)
        val themeImageFile = File("$levelFolder/$currentTheme/1Theme.png")
        val themeFolder = File("$levelFolder/$currentTheme")
        val themeFiles = themeFolder.listFiles()?.toList()


        if (themeFiles != null && themeFiles.size >= 5) {
            val correctAnswer = themeFiles.filter {
                it.name.equals("fruit4.png",true)||it.name.equals("fruit6.png",true)
            }.map { it.path }

            var themeFolders = levelFolder.listFiles()
                ?.filter { it.isDirectory && it.name != currentTheme } // Exclude the current theme
                ?.map { it.name }?.toMutableList()
            themeFolders= themeFolders?.let { getRandomSubset(it,10) }
            if (themeFolders != null && themeFolders.size >= 10) {
                val incorrectAnswer = mutableListOf<String>()
                themeFolders.forEach {
                    val randomThemeFolder = File("$levelFolder/$it")
                    val randomThemeFiles = randomThemeFolder.listFiles().filter { !it.name.equals("Thumbs.db") }?.toList()
                    if (randomThemeFiles != null && randomThemeFiles.isNotEmpty()) {
                        val incorrectFile = randomThemeFiles.random()
                        incorrectAnswer.add(incorrectFile.path)
                    }
                }

                return GameOc(
                    1, // Assuming you only want one level with the specified theme
                    1,
                    currentTheme,
                    themeImageFile.path,
                    correctAnswer,
                    incorrectAnswer
                )
            } else {
                println("没有足够的其他主题文件夹。")
            }
        } else {
            println("主题文件夹 $currentTheme 中的文件数量不足。")
        }

        return null
    }



    //TODO 第三关的YES NO 是反的 回去需要更新资源
    fun generateHardGames(path: String): List<GameOc> {
        val levelFolder = File(path)
        var themeFolders =
            levelFolder.listFiles()?.filter { it.isDirectory }?.map { it.name }?.toMutableList()

        val games = mutableListOf<GameOc>()
        val usedThemes = mutableListOf<String>()

        if (themeFolders != null && themeFolders.isNotEmpty()) {
            themeFolders=getRandomSubset(themeFolders,8)

            for (level in 3..8) {
                var currentTheme = themeFolders[level - 3]
                val themeImageFile = File("$levelFolder/$currentTheme/Theme$currentTheme.png")//
                val yesFolder = File("$levelFolder/$currentTheme")
                val yesFiles = yesFolder.listFiles { file ->
                    file.isDirectory && file.name.startsWith("yes", ignoreCase = true)
                }?.flatMap { it.listFiles()?.toMutableList() ?: emptyList() }

                val noFolder = File("$levelFolder/$currentTheme")
                val noFiles = noFolder.listFiles { file ->
                    file.isDirectory && file.name.startsWith("no", ignoreCase = true)
                }?.flatMap { it.listFiles()?.toMutableList() ?: emptyList() }

                if (yesFiles != null && noFiles != null && yesFiles.size >= 4 && noFiles.size >= 4) {
                    val shuffledYesFiles = yesFiles.shuffled()
                    val shuffledNoFiles = noFiles.shuffled()

                    var size=shuffledNoFiles.size
                    if(size>=8){
                        size=8
                    }
                    val correctAnswers = shuffledYesFiles.filter {  !it.name.equals("Thumbs.db") }.subList(0, 4).map { it.path }
                    var incorrectAnswers = shuffledNoFiles.subList(0, size).map { it.path }//可能存在不足8张

                   val remainingIncorrectAnswers = 8 - incorrectAnswers.size
                    //不足8张 从其他
                    if(remainingIncorrectAnswers>0){
                        val result = themeFolders.filterIndexed { index, _ -> index != (level - 3) }
                        for(i in 0..remainingIncorrectAnswers-1){
                            val incorrectTheme = result.get(i)
                            val incorrectThemeFolder = File("$levelFolder/$incorrectTheme")
                            val yesFiles = incorrectThemeFolder.listFiles { file ->
                                file.isDirectory && file.name.startsWith("yes", ignoreCase = true)
                            }?.flatMap { it.listFiles()?.toMutableList() ?: emptyList() }
                            if (yesFiles != null && yesFiles.isNotEmpty()) {
                                (incorrectAnswers as ArrayList<String>).add(yesFiles.get(0).path)
                            }
                        }
                    }
//                    var additionalIncorrectAnswers = 0
//
//                    while (remainingIncorrectAnswers > 0 && additionalIncorrectAnswers < themeFolders.size) {
//                        val nextTheme = themeFolders.filter { !usedThemes.contains(it) }.random()
//                        val nextNoFiles = File("$levelFolder/$nextTheme").listFiles { file ->
//                            file.isDirectory && file.name.startsWith("no", ignoreCase = true)
//                        }?.flatMap { it.listFiles()?.toMutableList() ?: emptyList() }
//
//                        if (nextNoFiles != null && nextNoFiles.isNotEmpty()) {
//                            val remainingNeeded = remainingIncorrectAnswers.coerceAtMost(nextNoFiles.size)
//                            incorrectAnswers = incorrectAnswers.plus(nextNoFiles.subList(0, remainingNeeded).map { it.path })
//                            additionalIncorrectAnswers++
//                            usedThemes.add(nextTheme)
//                        }
//                    }
//                    if(Oc?.ocTheme?.get(currentTheme)?.isNotEmpty() == true) {
//                        currentTheme = Oc.ocTheme[currentTheme].toString()
//                    }
                    games.add(
                        GameOc(
                            level-2,
                            1,
                            currentTheme,
                            themeImageFile.path,
                            correctAnswers,
                            incorrectAnswers
                        )
                    )
                }
            }
        } else {
            println("level文件夹为空或不存在主题文件夹。")
        }

        return games
    }



}